import clone from "@/common/clone"
const users = [
    {
        id: 1,
        token: '6666666666666666666666666666666666666666',
        avatar: 'https://todo-dev.oss-accelerate.aliyuncs.com/todo-dev/jm/9g/7y/2021111_19022.jpg',
        username: 'admin',
        password: '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92',
        state: 1,//是否启用，0否 1是
        createdTime: '2021-11-30 15:10:10',  // 创建时间
        roles: [1],
        boolDel: 1,

    }
]

const roles = localStorage.roles ? JSON.parse(localStorage.roles) : [
    {
        id: 1,
        roleName: '管理员',
        createdTime: '2021-11-30 15:10:10',  // 创建时间
        remark: '管理员',
        boolDel: 1,
        permissions: [
            1,
            3,
            5,
            4,
            2,
            100,
            6,
            7,
            18,
            8,
            9,
            10,
            11,
            12,
            13,
            15,
            16,
            17,
            14,
        ]
    }
]





let menuList = [
    {
        id: 1,
        path: '/dashboard',
        name: 'dashboard',
        meta: { title: '首页', icon: 'el-icon-s-home' },
    },
    {
        id: 2,
        path: '/nested',
        name: 'nested',
        meta: { title: '嵌套路由', icon: 'el-icon-s-operation', breadcrumbClick: false },
        children: [
            {
                id: 3,
                path: '/nested/menu1',
                name: 'menu1',
                meta: { title: '菜单1' },
            },
            {
                id: 4,
                meta: { title: '菜单2', breadcrumbClick: false },
                path: '/nested/menu2',
                name: 'menu2',
                children: [
                    {
                        id: 5,
                        path: '/nested/menu2/menu2-1',
                        name: 'menu2-1',
                        meta: { title: '菜单2-1' },
                    },
                ]
            },
        ]
    },
    {
        id: 18,
        path: '/table',
        name: 'table',
        meta: { title: '表格示例', icon: 'el-icon-s-grid', breadcrumbClick: false },
        children: [
            {
                id: 6,
                path: '/table/basicGrid',
                name: 'basicGrid',
                meta: { title: '基础表格' },
                children: [
                    // {
                    //     path: "basicGridDetails",
                    //     name: 'basicGridDetails',
                    //     meta: { title: '详情', activeMenu: 'basicGrid' },
                    //     hidden: true,
                    // }
                    {
                        id: 100,
                        title: '编辑',
                        isButton: true,
                    },
                    {
                        id: 101,
                        title: '删除',
                        isButton: true,
                    }
                ]
            },
            {
                id: 7,
                path: '/table/grid',
                name: 'grid',
                meta: { title: 'grid' },
            },
        ]
    },
    {
        id: 8,
        path: '/form',
        name: 'form',
        meta: { title: 'form演示', icon: 'el-icon-s-order' },
    },
    {
        id: 9,
        path: '/drag',
        name: 'drag',
        meta: { title: '拖拽示例', icon: 'el-icon-s-check' },
    },
    {
        id: 10,
        path: '/splitPane',
        name: 'splitPane',
        meta: { title: '宽度拖拽示例', icon: 'el-icon-star-on' },
    },
    {
        id: 11,
        path: '/tinymce',
        name: 'tinymce',
        meta: { title: '富文本演示', icon: 'el-icon-s-check' },
    },
    {
        id: 12,
        path: '/drawer',
        name: 'drawer',
        meta: { title: '抽屉演示', icon: 'el-icon-s-check' },
    },
    {
        id: 13,
        path: '/tree',
        name: 'tree',
        meta: { title: 'tree演示', icon: 'el-icon-s-check' },
    },
    {
        id: 14,
        path: '/systemManagement',
        name: 'systemManagement',
        meta: { title: '系统管理', icon: 'el-icon-s-operation', breadcrumbClick: false },
        children: [
            {
                id: 15,
                path: '/systemManagement/user',
                name: 'user',
                meta: { title: '用户管理' },
            },
            {
                id: 16,
                path: '/systemManagement/menu',
                name: 'menu',
                meta: { title: '菜单管理' },
            },
            {
                id: 17,
                path: '/systemManagement/role',
                name: 'role',
                meta: { title: '角色管理' },
            },
        ]
    },
]

function filterList (item, username, region) {
    let flag = true
    if (item.boolDel === 1) {
        if (username && item.username.includes(username) === false) {
            flag = false
        }
    } else {
        flag = false
    }

    return flag
}





export default [
    // user login
    {
        url: '/user/login',
        type: 'post',
        response: config => {
            const { username, password } = config.body
            let userinfo = users.find(item => item.username === username && item.password === password);
            // mock error
            if (!userinfo) {
                return {
                    code: 401,
                    msg: '用户名或密码错误！'
                }
            }
            return {
                code: 0,
                result: userinfo
            }
        }
    },
    {
        url: '/user/list',
        type: 'post',
        response: config => {
            const { page, limit, username } = config.body;
            let list = users.filter(item => {
                return filterList(item, username);
            })
            let records = list.slice((page - 1) * limit, page * limit)
            return {
                code: 0,
                result: {
                    records,
                    total: list.length
                }
            }
        }
    },
    {
        url: '/user/add',
        type: 'post',
        response: config => {
            let params = config.body
            params.boolDel = 1;
            params.id = users.length + 1;
            params.createdTime = '2021-11-30 15:10:10';
            params.token = '6666666666666666666666666666666666666666';
            params.avatar = 'https://todo-dev.oss-accelerate.aliyuncs.com/todo-dev/jm/9g/7y/2021111_19022.jpg';
            users.unshift(params);
            return {
                code: 0,
                result: true
            }
        }
    },
    {
        url: '/role/list',
        type: 'post',
        response: config => {
            const { page, limit } = config.body;
            let list = roles.filter(item => {
                return item.boolDel === 1
            })
            let records = list.slice((page - 1) * limit, page * limit)
            return {
                code: 0,
                result: {
                    records,
                    total: list.length
                }
            }
        }
    },
    {
        url: '/role/all',
        type: 'post',
        response: config => {
            return {
                code: 0,
                result: roles
            }
        }
    },
    {
        url: '/role/add',
        type: 'post',
        response: config => {
            let params = config.body
            params.boolDel = 1;
            params.id = roles.length + 1;
            params.createdTime = '2021-11-30 15:10:10'
            roles.unshift(params);
            localStorage.roles = JSON.stringify(roles)
            return {
                code: 0,
                result: true
            }
        }
    },
    {
        url: '/role/edit',
        type: 'post',
        response: config => {
            let params = config.body
            roles.forEach((item, i) => {
                if (item.id === params.id) {
                    roles[i] = params
                }
            });
            localStorage.roles = JSON.stringify(roles)
            return {
                code: 0,
                result: true
            }
        }
    },
    {
        url: '/menu/list',
        type: 'post',
        response: config => {
            return {
                code: 0,
                result: menuList
            }
        }
    },
    {
        url: '/menu/add',
        type: 'post',
        response: config => {
            let params = config.body
            console.log(params);
            menuList = params;
            return {
                code: 0,
                result: true
            }
        }
    },
    {
        url: '/user/getJurisdiction',
        type: 'post',
        response: config => {
            let params = config.body
            let permissionsList = roles.filter(item => {
                return params.roles.includes(item.id)
            }).map(item => item.permissions)
            let roleList = permissionsList.flat(Infinity);
            roleList = [...new Set(roleList)];
            let list = clone(menuList)
            let menus = getMuneList(list, roleList);
            console.log(menus);
            return {
                code: 0,
                result: menus
            }
        }
    },

]

function getMuneList (list, permissionsList) {
    return list.filter(item => {
        item.children?.[0] && (item.children = getMuneList(item.children, permissionsList))
        return permissionsList.includes(item.id) || item.children?.[0]
    })
}
